package com.ithinkstudio.jeez.service.impl;

import com.ithinkstudio.jeez.dao.IUserRepository;
import com.ithinkstudio.jeez.entity.User;
import com.ithinkstudio.jeez.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: springboot-mybatis
 * @description: 用户服务层
 * @author: fengzp
 * @create: 2020/04/14
 */
@Service
public class UserService implements IUserService {

    @Resource
    private IUserRepository userRepository;


    /**
     * 根据ID查询用户
     *
     * @param id
     * @return
     */
    @Override
    public User findById(Integer id) {
        User user = userRepository.findById(id);
        return user;
    }

    /**
     * 新增用户
     *
     * @param user
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertUser(User user) {
        userRepository.insert(user);
    }

    /**
     * 更新用户
     *
     * @param user
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUser(User user) {
        userRepository.update(user);
    }

    /**
     * 删除用户
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteUser(Integer id) {
        userRepository.delete(id);
    }

    /**
     * 查询列表
     * @return
     */
    @Override
    public List<User> list() {
        String hql = "from User u where u.id > ?0";
        Object[] args = new Object[] {3};
        List<User> userList = userRepository.list(hql, args);
        return userList;
    }
}
